/*
 * Copyright (c) 2016 MariaDB Corporation Ab
 * Copyright (c) 2023 MariaDB plc, Finnish Branch
 *
 * Use of this software is governed by the Business Source License included
 * in the LICENSE.TXT file and at www.mariadb.com/bsl11.
 *
 * Change Date: 2028-05-14
 *
 * On the date above, in accordance with the Business Source License, use
 * of this software will be governed by version 2 or later of the General
 * Public License.
 */

/**
 * @file bug587.cpp  regression case for the bug 587 ( "Hint filter don't work if listed before regex filter
 * in configuration file" )
 *
 * - Maxscale.cnf
 * @verbatim
 *  [hints]
 *  type=filter
 *  module=hintfilter
 *
 *  [regex]
 *  type=filter
 *  module=regexfilter
 *  match=fetch
 *  replace=select
 *
 *  [RW Split Router]
 *  type=service
 *  router= readwritesplit
 *  servers=server1,     server2,              server3,server4
 *  user=skysql
 *  passwd=skysql
 *  max_slave_connections=100%
 *  use_sql_variables_in=all
 *  router_options=slave_selection_criteria=LEAST_BEHIND_MASTER
 *  filters=hints|regex
 *  @endverbatim
 * - second test (bug587_1) is executed with "filters=regex|hints" (dffeent order of filters)
 * - check if hints filter working by executing and comparing results:
 *  + via RWSPLIT: "select @@server_id; -- maxscale route to server server%d" (%d - node number)
 *  + directly to backend node "select @@server_id;"
 * - do the same test with "filters=regex|hints" "filters=hints|regex"
 */

/*
 *  Vilho Raatikka 2014-10-21 19:12:33 UTC
 *  If filters and rwsplit are configured as follows, hints don't work.
 *
 *  [hints]
 *  type=filter
 *  module=hintfilter
 *
 *  [regex]
 *  type=filter
 *  module=regexfilter
 *  match=fetch
 *  replace=select
 *
 *  [RW Split Router]
 *  type=service
 *  router=readwritesplit
 *  servers=server1,server2,server3,server4
 *  max_slave_connections=100%
 *  use_sql_variables_in=all
 *  user=maxuser
 *  passwd=maxpwd
 *  filters=hints|regex
 *
 *  Changing filters=regex|hints makes it work. This is due to processing order. Regex filter drops hint off.
 *  Comment 1 Vilho Raatikka 2014-10-23 18:08:07 UTC
 *  buffer.c:gwbuf_make_contiguous: hint wasn't duplicated to new GWBUF struct. As a result hints were lost if
 * query rewriting resulted in longer query than the original.
 */


#include <iostream>
#include <unistd.h>
#include <maxtest/testconnections.hh>

using namespace std;

int main(int argc, char* argv[])
{
    TestConnections* Test = new TestConnections(argc, argv);
    Test->reset_timeout();
    Test->repl->connect();
    Test->maxscale->connect_maxscale();

    char server_id[256];
    char server_id_d[256];

    char hint_sql[128];

    for (int i = 1; i < 25; i++)
    {
        for (int j = 0; j < Test->repl->N; j++)
        {
            Test->reset_timeout();
            sprintf(hint_sql, "select @@server_id; -- maxscale route to server server%d", j + 1);
            Test->tprintf("%s\n", hint_sql);

            find_field(Test->maxscale->conn_rwsplit, hint_sql, (char*) "@@server_id", &server_id[0]);
            find_field(Test->repl->nodes[j],
                       (char*) "select @@server_id;",
                       (char*) "@@server_id",
                       &server_id_d[0]);

            Test->tprintf("server%d ID from Maxscale: \t%s\n", j + 1, server_id);
            Test->tprintf("server%d ID directly from node: \t%s\n", j + 1, server_id_d);

            Test->add_result(strcmp(server_id, server_id_d), "Hints does not work!\n");
        }
    }

    Test->maxscale->close_maxscale_connections();
    Test->repl->close_connections();

    Test->check_maxscale_alive();

    int rval = Test->global_result;
    delete Test;
    return rval;
}
